package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsImpl implements NewsDao {
    @Override
    public List<NewsModel> getList() {
//        limit 3查询3条数据
//        order by id 排序
//        desc降序  asc升序
        String sql = "select * from tab_news order by id desc limit 3";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<NewsModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NewsModel newsModel = new NewsModel();
                newsModel.setId(resultSet.getInt("id"));
                newsModel.setHref(resultSet.getString("href"));
                newsModel.setTitle(resultSet.getString("title"));
                newsModel.setContent(resultSet.getString("content"));
                list.add(newsModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
